﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace Suucha.Expressions
{
    /// <summary>
    /// 排序
    /// </summary>
    public class SuuchaOrderBy
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public SuuchaOrderBy()
            : this("", true)
        {
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name">成员属性名称</param>
        /// <param name="asc">如果是正序为true</param>
        public SuuchaOrderBy(string name, bool asc)
        {
            Member = new SuuchaMemberExpression(name);
            Asc = asc;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="member">成员属性表达式</param>
        /// <param name="asc">如果是正序为true</param>
        public SuuchaOrderBy(SuuchaMemberExpression member, bool asc)
        {
            Member = member;
            Asc = asc;
        }
        /// <summary>
        /// 成员表达式
        /// </summary>
        public SuuchaMemberExpression Member { get; set; }
        /// <summary>
        /// 正序为true
        /// </summary>
        public bool Asc { get; set; }
        /// <summary>
        /// 添加正序排序
        /// </summary>
        /// <param name="name">成员属性名称</param>
        /// <returns></returns>
        public static List<SuuchaOrderBy> OrderBy(string name)
        {
            List<SuuchaOrderBy> list = new List<SuuchaOrderBy>();
            list.Add(new SuuchaOrderBy(name, true));
            return list;
        }
        /// <summary>
        /// 添加反序排序
        /// </summary>
        /// <param name="name">成员属性名称</param>
        /// <returns></returns>
        public static List<SuuchaOrderBy> OrderByDescending(string name)
        {
            List<SuuchaOrderBy> list = new List<SuuchaOrderBy>();
            list.Add(new SuuchaOrderBy(name, false));
            return list;
        }
        /// <summary>
        /// 添加正序排序
        /// </summary>
        /// <param name="member">成员属性表达式</param>
        /// <returns></returns>
        public static List<SuuchaOrderBy> OrderBy(SuuchaMemberExpression member)
        {
            List<SuuchaOrderBy> list = new List<SuuchaOrderBy>();
            list.Add(new SuuchaOrderBy(member, true));
            return list;
        }
        /// <summary>
        /// 添加反序排序
        /// </summary>
        /// <param name="member">成员属性表达式</param>
        /// <returns></returns>
        public static List<SuuchaOrderBy> OrderByDescending(SuuchaMemberExpression member)
        {
            List<SuuchaOrderBy> list = new List<SuuchaOrderBy>();
            list.Add(new SuuchaOrderBy(member, false));
            return list;
        }
    }

    /// <summary>
    /// 泛型排序类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SuuchaOrderBy<T>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="selector">成员选择器</param>
        /// <param name="asc">如果是正序为true</param>
        public SuuchaOrderBy(Expression<Func<T,object>> selector, bool asc)
        {
            Selector = selector;
            Asc = asc;
        }
        /// <summary>
        /// 成员选择器
        /// </summary>
        public Expression<Func<T, object>> Selector { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        public bool Asc { get; private set; }
        /// <summary>
        /// 添加正序排序
        /// </summary>
        /// <param name="selector">成员选择器</param>
        /// <returns></returns>
        public static List<SuuchaOrderBy<T>> OrderBy(Expression<Func<T, object>> selector)
        {
            List<SuuchaOrderBy<T>> list = new List<SuuchaOrderBy<T>>();
            list.Add(new SuuchaOrderBy<T>(selector, true));
            return list;
        }
        /// <summary>
        /// 添加反序排序
        /// </summary>
        /// <param name="selector">成员选择器</param>
        /// <returns></returns>
        public static List<SuuchaOrderBy<T>> OrderByDescending(Expression<Func<T, object>> selector)
        {
            List<SuuchaOrderBy<T>> list = new List<SuuchaOrderBy<T>>();
            list.Add(new SuuchaOrderBy<T>(selector, false));
            return list;
        }
    }
}
